<?php
session_start();
if(isset($_POST['txtHiddenCode'])) {
    $pass = $_POST['txtPassword'];
    $email = $_POST['txtEmail'];
    $sql = "select name,email from tbl_member where email ='".$email."' and pwd='".$pass."'";
    $members = mysql_query($sql, $conn);
    
    if(mysql_num_rows($members)) {
        $_SESSION['email'] = $email;
        location("/");
    } else {
        ?>
        <script type="text/javascript">
            alert("Hòm thư hoặc mật khẩu không hợp lệ!");
        </script>
        <?php
    }
}

?>
<?php
function location($url)
{?>
<script type="text/javascript">
window.location = "<?=$url?>";
</script>
<?php }
?>
<form action="/khohangtot/login.htm" method="post" name="frmLogin" id="frmLogin">
<div id="form_base">
	<div class="title_left_bg"></div><div class="title_center_bg"><div class="title_center_icon"></div>Đăng nhập</div><div class="title_right_bg"></div>
        <div class="form">
            <table>
                <tr><th>Hòm thư : </th><td><input type="text" name="txtEmail" class="text"/><span class="error">*</span></td></tr>
                <tr><th>Mật khẩu : </th><td><input type="password" name="txtPassword" class="text"/><span class="error">*</span></td></tr>
                <tr><td>&nbsp;</td><td><a href="/khohangtot/forgot-password.htm">Lấy lại mật khẩu</a></td></tr>
                <tr>
                    <th>
                        <div class="button" id="LamLai">
                            <a href="javascript:void(0);">
                                <div class="btn_left_bg"></div><div class="btn_center_bg">Làm lại</div><div class="btn_right_bg"></div>
                            </a>
                        </div><!--end .button-->
                    </th>
                    <td>
                        <div class="button" id="Dn">
                            <a href="javascript:void(0);">
                                <div class="btn_left_bg"></div><div class="btn_center_bg">Đăng nhập</div><div class="btn_right_bg"></div>
                            </a>
                        </div><!--end .button-->
                    </td>
                </tr>
            </table>
        </div><!--end .form-->
</div><!--end #form_base-->
    <input type="hidden" name="txtHiddenCode" id="txtHiddenCode"/>
</form><!--end form-->
<script type="text/javascript">
    function echeck(str) {
        var atpos=str.indexOf("@");
        var dotpos=str.lastIndexOf(".");
        if (atpos<1 || dotpos<atpos+2 || dotpos+2>=str.length)
        {
            return false;
        }
        return true;					
	}
    function chkFrmDn(){
        var frm = document.getElementById("frmLogin");
        var err = "";
        var email = frm["txtEmail"].value;
        var pass = frm["txtPassword"].value;
        
        
        if(!echeck(email)) {
            err += "Hòm thư không hợp lệ !\n";
        }
        if(pass.length < 6) {
            err += "Mật khẩu phải nhiều hơn 6 ký tự !\n"
        }
        if(err != "") {
            alert(err);
            return false;
        }
        return true;
    }
    $(function(){
        $("#LamLai").click(function(){
            var frm = document.getElementById("frmRegister");
            frm["txtEmail"].value = "";
            frm["txtPassword"].value = "";
        });
        $("#Dn").click(function(){
            if(chkFrmDn()) {
                $("#txtHiddenCode").val("dn");
                $("#frmLogin").submit();
            }
        });
    });
</script>